<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_konten extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'id_konten';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		//$nama_desa = isset($_POST['nama_desa']) ? strval($_POST['nama_desa']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('*');
			$this->db->from('konten');
			//$this->db->where('status','A');
			
			//if($nama_desa != ''){
			//	$this->db->like('nama_desa', $nama_desa, 'both');
			//}
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
		
	function togrid($data, $count=0){
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
			$response->total = $count;
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}

	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('jenis_surat', $data['jenis_surat']);
		$this->db->set('paragraf_1', $data['paragraf_1']);
		$this->db->set('paragraf_2', $data['paragraf_2']);
		$this->db->set('paragraf_3', $data['paragraf_3']);
		$this->db->set('paragraf_4', $data['paragraf_4']);
		
		$result = $this->db->insert('konten');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}

	function getDataEdit($id_konten){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('konten');
		$this->db->where('id_konten', $id_konten);
		return $this->db->get();
	}

	function UpdateOnDb($data){
		$this->db->flush_cache();
		//$this->db->set('id_desa', $data['id_desa']);
		$this->db->set('jenis_surat', $data['jenis_surat']);
		$this->db->set('paragraf_1', $data['paragraf_1']);
		$this->db->set('paragraf_2', $data['paragraf_2']);
		$this->db->set('paragraf_3', $data['paragraf_3']);
		$this->db->set('paragraf_4', $data['paragraf_4']);
		$this->db->where('id_konten', $data['id_konten']);

		$result = $this->db->update('konten');

		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
}

?>